<template>
  <div class="cgjhadd">
    <el-form ref="form" :model="form" :rules="rules" label-width="20%">
      <div class="contain">
        <div class="left">
          <el-collapse v-model="activeNames">
            <el-collapse-item title="申请信息" name="1" id="shenqing">
              <div class="form-box">
                <el-form-item label="企业名称" prop="enterpriseName">
                  <el-input
                    v-model="form.enterpriseName"
                    placeholder="请输入企业名称"
                  />
                </el-form-item>

                <el-form-item label="企业联系人" prop="enterpriseContact">
                  <el-input
                    v-model="form.enterpriseContact"
                    placeholder="请输入企业联系人"
                  />
                </el-form-item>

                <el-form-item label="企业联系人电话" prop="contactPhone">
                  <el-input
                    v-model="form.contactPhone"
                    placeholder="请输入企业联系人电话"
                  />
                </el-form-item>
                <el-form-item label="企业类型" prop="enterType">
                  <el-select v-model="form.enterType" >
                    <el-option :value="item.dictValue" :label="item.dictLabel" v-for="(item,index) in enterTypeOpts" :key="index"></el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="行业类型" prop="typeId" >
                  <treeselect    v-model="form.typeId" :options="qyEnterpriseTypeList" :normalizer="normalizer"
                               placeholder="选择行业类型"/>
                </el-form-item>

                <el-form-item label="生产类型" prop="productionType">
                  <el-select filterable v-model="form.productionType" multiple placeholder="请选择生产类型">
                    <el-option
                      v-for="item in qyEnterpriseProductionTypes"
                      :key="item.id"
                      :label="item.proName"
                      :value="item.proName">
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="企业法人" prop="legalPerson">
                  <el-input
                    v-model="form.legalPerson"
                    placeholder="请输入企业法人"
                  />
                </el-form-item>
                <el-form-item label="法人联系电话" prop="legalPhone">
                  <el-input
                    v-model="form.legalPhone"
                    placeholder="请输入法人联系电话"
                  />
                </el-form-item>
                <el-form-item label="工商注册时间" prop="registrationDate">
                  <el-date-picker
                    clearable
                    size="small"
                    v-model="form.registrationDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择工商注册时间"
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="税务登记时间" prop="taxRegistrationDate">
                  <el-date-picker
                    clearable
                    size="small"
                    v-model="form.taxRegistrationDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择税务登记时间"
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="从业人员数量" prop="enterpriseScale">
                  <el-input
                    v-model="form.enterpriseScale"
                    placeholder="请输入从业人员数量"
                  />
                </el-form-item>
                <el-form-item label="环评批复时间" prop="approvalDate">
                  <el-date-picker
                    clearable
                    size="small"
                    v-model="form.approvalDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择环评批复时间"
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="环评验收时间" prop="acceptanceDate">
                  <el-date-picker
                    clearable
                    size="small"
                    v-model="form.acceptanceDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择环评验收时间"
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="生产状况">
                  <el-input
                    v-model="form.productionStatus"
                    placeholder="请输入生产状况"
                  />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                  <el-input
                    v-model="form.remark"
                    type="textarea"
                    placeholder="请输入内容"
                  />
                </el-form-item>
              </div>
            </el-collapse-item>

            <el-collapse-item title="其他费用" name="2" id="otherfee">
              <div class="form-box">
                <el-form-item label="垃圾费" prop="garbageFee">
                  <el-input
                    v-model="form.garbageFee"
                    placeholder="请输入垃圾费"
                  />
                </el-form-item>
                <el-form-item label="污水处理费" prop="sewageFee">
                  <el-input
                    v-model="form.sewageFee"
                    placeholder="请输入污水处理费"
                  />
                </el-form-item>

                <el-form-item label="电梯运行费" prop="elevatorRunFee">
                  <el-input
                    v-model="form.elevatorRunFee"
                    placeholder="请输入电梯运行费"
                  />
                </el-form-item>
                <el-form-item label="电梯维修费" prop="elevatorRepairFee">
                  <el-input
                    v-model="form.elevatorRepairFee"
                    placeholder="请输入电梯维修费"
                  />
                </el-form-item>
              </div>

            </el-collapse-item>

            <el-collapse-item title="附件上传" name="3" id="fujian">
              <div class="form-box">
                <el-form-item label="附件">
                  <FileUpload ref="fileupload" :fileListTem="form.fileIdList" :fileSize="fileSize" :limit="20"/>
                </el-form-item>
              </div>
            </el-collapse-item>
          </el-collapse>
        </div>
        <div class="right">
          <div class="my-steps-box">
            <div
              :class="['step-item', stepSelect == index ? 'step-active' : '']"
              v-for="(item, index) in step"
              :key="index"
            >
              <div class="top-title" @click="jump(item.to, index)">
                <div class="cricle"></div>
                <span class="text">{{ item.text }}</span>
              </div>
              <div class="line"></div>
            </div>
          </div>
        </div>
      </div>
      <div class="big-foot-fixed">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-form>
  </div>
</template>
<script>
import {
  getQyEnterprise,
  addQyEnterprise,
  updateQyEnterprise,
} from "@/api/smartParks/merchants/merchantsCompanySettlein";
import {listZsCustomer} from "@/api/smartParks/merchants/merchantsCustomer";
import FileUpload from "@/views/smartParks/parkAssets/components/FileUpload.vue";
import {listQyEnterpriseType} from "@/api/smartParks/merchants/enterpriseType";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listQyEnterpriseProductionType} from "@/api/system/qyEnterpriseProductionType";
import {getDicts} from "@/api/system/dict/data";

export default {
  name: "QyEnterpriseAdd",
  components: {
    FileUpload,
    Treeselect
  },
  data() {
    return {
      fileSize: 20,//附件大小
      activeNames: ["1", "2","3"],
      step: [
        {
          to: "shenqing",
          text: "申请信息",
        },
        {
          to: "otherfee",
          text: "其他费用",
        },
        {
          to: "fujian",
          text: "上传附件",
        },
      ],
      enterTypeOpts:[],
      stepSelect: 0,
      qyEnterpriseProductionTypes:[],
      // 表单参数
      form: {
        id: null,
        enterpriseName: null,
        legalPerson: null,
        legalPhone: null,
        enterpriseContact: null,
        contactPhone: null,
        enterpriseType: null,
        enterType:null,
        productionType:null,
        registrationDate: null,
        mainProduct: null,
        enterpriseScale: null,
        taxRegistrationDate: null,
        approvalDate: null,
        acceptanceDate: null,
        createBy: null,
        productionStatus: null,
        createTime: null,
        address: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        tenantId: null,
        fileIdList: [],
      },
      zsCustomerList: [], //或缺企业
      // 表单校验
      rules: {
        enterpriseName: [
          {required: true, message: "企业名称不能为空", trigger: "blur"},
        ],
        legalPerson: [
          {required: true, message: "企业法人不能为空", trigger: "blur"},
        ],
        legalPhone: [
          {required: true, message: "法人联系电话不能为空", trigger: "blur"},
        ],
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      dialogdeviceVisible: false, //房源
      loading: false,
      qyEnterpriseTypeList: []
    };
  },
  beforeRouteLeave(to, from, next) {
    this.$store.dispatch("tagsView/delView", this.$route);
    next();
  },
  created() {
    getDicts("enter_type").then(res=>{
      this.enterTypeOpts=res.data
    })
    listQyEnterpriseProductionType().then(res=>{
      this.qyEnterpriseProductionTypes=res.rows
    })
    listQyEnterpriseType().then((response) => {
      this.qyEnterpriseTypeList = this.handleTree(response.rows, "id");
    });
    getQyEnterprise(this.$route.query.id).then((response) => {
      this.form = response.data;
      this.form.productionType=response.data.productionType.split(",")
      this.form.fileIdList = [];
      this.form.fileList.forEach((element) => {
        this.form.fileIdList.push({
          name: element.name,
          id: element.id,
          url: "http://" + element.url,
        });
      });
    });
    this.getmaintainList(); //列表
  },
  methods: {
    jump(domId, index) {
      this.stepSelect = index;
      //页面滚动了的距离
      let height =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      let dom = document.getElementById(domId);
      // let domHeight = dom.offsetTop + 60;
      let domHeight = dom.offsetTop - 104;
      //滚动距离计算
      var S = Number(height) - Number(domHeight);
      //判断上滚还是下滚
      if (S < 0) {
        //下滚
        S = Math.abs(S);
        window.scrollBy({
          top: S,
          behavior: "smooth",
        });
      } else if (S == 0) {
        //不滚
        window.scrollBy({
          top: 0,
          behavior: "smooth",
        });
      } else {
        //上滚
        S = -S;
        window.scrollBy({
          top: S,
          behavior: "smooth",
        });
      }
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.enterpriseType,
        children: node.children
      };
    },
    changeEnter() {
      if (this.form.customerId) {
        let itemTep = this.zsCustomerList.filter((item) => {
          return item.id == this.form.customerId;
        });
        this.form.enterpriseName = itemTep[0].enterpriseName
          ? itemTep[0].enterpriseName
          : undefined;
        this.form.enterpriseContact = itemTep[0].enterpriseContact
          ? itemTep[0].enterpriseContact
          : undefined;
        this.form.contactPhone = itemTep[0].contactPhone
          ? itemTep[0].contactPhone
          : undefined;
        this.form.enterpriseType = itemTep[0].enterpriseType
          ? itemTep[0].enterpriseType
          : undefined;
      } else {
        this.form.enterpriseName = undefined;
        this.form.enterpriseContact = undefined;
        this.form.contactPhone = undefined;
        this.form.enterpriseType = undefined;
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterpriseName: null,
        legalPerson: null,
        legalPhone: null,
        enterpriseContact: null,
        contactPhone: null,
        enterpriseType: null,
        registrationDate: null,
        mainProduct: null,
        enterpriseScale: null,
        taxRegistrationDate: null,
        approvalDate: null,
        acceptanceDate: null,
        createBy: null,
        productionStatus: null,
        createTime: null,
        address: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        tenantId: null,
        fileIdList: [],
        leaseRecord: {
          roomIdList: [], //
          elevatorFeeId: "", //电梯
          facilityFeeId: "", //设施
          fixedFeeId: "", //固定费用
          officeFeeId: "", //办公费用
          propertyFeeId: "", //物业
          parkingFeeId: "", //停车费用
        },
        enterType: null,
        productionType: null
      };

      this.resetForm("form");
    },
    // 取消按钮
    cancel() {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.go(-1);
    },

    //获取列表
    getmaintainList() {
      //选择企业
      listZsCustomer().then((response) => {
        this.zsCustomerList = response.rows;
      });

    },

    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {


          let data = JSON.parse(JSON.stringify(this.form));
          data.fileIdList = this.$refs.fileupload.fileList.map(
            (item) => item.id
          );
          if (this.form.id != undefined) {
            this.form.productionType=this.form.productionType.toString()
            updateQyEnterprise(data).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                setTimeout(() => {
                  this.$store.dispatch("tagsView/delView", this.$route);
                  this.$router.go(-1);
                }, 3000);
              }
            });
          } else {
            addQyEnterprise(data).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.reset();
              }
            });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-select,
.el-date-editor {
  width: 100%;
}

.big-foot-fixed {
  text-align: center;
  padding-bottom: 20px;
}

.cgjhadd {
  overflow: hidden;
  background: #f7f8f8;
  min-height: calc(100vh - 84px);
  height: auto;

  .top {
    height: 40px;
    box-sizing: border-box;
    background: #409eff !important;
    color: #fff;
    padding-left: 20px;
    display: flex;
    align-items: center;
    font-size: 12px;
    justify-content: space-between;

    .submit {
      padding: 3px 10px;
      border-radius: 10px;
      background: #fff;
      color: #409eff;
      cursor: pointer;
      margin-right: 100px;
    }
  }
}

.contain {
  height: auto !important;
  margin: 20px 20px 0px 20px;
  padding-bottom: 20px;
  display: flex;

  .left {
    .text-height {
      height: 80px;

      ::v-deep .el-form-item__content {
        height: 80px;
      }
    }

    .lrAbout {
      display: flex;
      justify-content: space-between;
    }

    width: 90%;

    ::v-deep .el-collapse {
      border-bottom: 0px;
    }

    ::v-deep .el-collapse-item {
      background: #fff;
      font-size: 12px;
      margin-bottom: 20px;

      .el-collapse-item__header,
      .el-collapse-item__wrap {
        background: "";
      }

      .el-collapse-item__header {
        position: relative;
        height: 44px;
        padding-left: 20px;
      }

      .el-collapse-item__header:before {
        position: absolute;
        content: "";
        width: 2px;
        height: 18px;
        background-color: #409eff;
        left: 0;
        top: 13px;
      }

      .el-collapse-item__wrap {
        border-top: 1px solid #e6ebf5;
      }

      .el-collapse-item__content {
        padding-top: 25px;
      }

      .form-box1 {
        width: 100%;
        padding: 0 20px;

        .el-button {
          margin-top: 10px;
        }

        .el-button:first-child {
          margin-top: -10px;
        }

        .el-table {
          margin-top: 10px;
        }
      }

      .form-box {
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        padding: 0 20px;

        .el-form-item {
          width: 48%;
        }

        .el-form-item__label {
          font-size: 12px;
          font-weight: normal;
        }

        .el-form-item:nth-child(2n) {
          margin-left: 4%;
        }
      }
    }

    ::v-deep .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }

    ::v-deep .avatar-uploader .el-upload:hover {
      border-color: #409eff;
    }

    ::v-deep .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }

    ::v-deep .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
  }

  .right {
    width: 10%;

    .my-steps-box {
      font-size: 12px;
      position: fixed;
      right: 30px;
      // top: 104px;
      .step-item {
        margin-bottom: 30px;
        position: relative;

        .top-title {
          display: flex;
          cursor: pointer;

          .cricle {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            margin-right: 5px;
            background: #e8eaec;
            box-sizing: border-box;
            // background: red;
            border: 2px solid #f7f8f8;
          }
        }

        .line {
          position: absolute;
          width: 2px;
          height: 30px;
          top: 12px;
          left: 5px;
          background: #e8eaec;
        }
      }

      .step-item.step-active {
        .cricle {
          border: 2px solid #d5e7f9;
          background-color: #2d8cf0;
        }

        .text {
          color: #2d8cf0;
        }
      }

      .step-item:nth-last-child(1) .line {
        display: none;
      }
    }
  }
}
</style>
